package com.tao.china.biz.jdbc.dao.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.tao.china.biz.domain.Porxy;
import com.tao.china.biz.jdbc.dao.PorxyDao;

/**
 * @author liuchao
 */
public class PorxyDaoImpl implements PorxyDao {

    @Override
    public void importPorxyList(String filePath) {
        
         if( null != filePath ){
             File file = new File(filePath);
             if( null != file ){
                 BufferedReader reader = null;
                 try {
                     reader = new BufferedReader(new FileReader(file));
                     String tempString = null;
                     // 一次读入一行，直到读入null为文件结束
                     while ((tempString = reader.readLine()) != null) {
                         if( null != tempString ){
                             String ip = tempString.substring(0, tempString.indexOf(":"));
                             int port = Integer.parseInt( tempString.substring(tempString.indexOf(":")+1, tempString.length()));
                             if( ip != null && port != 0 ){
                             }
                         }
                     }
                     reader.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 } finally {
                     if (reader != null) {
                         try {
                             reader.close();
                         } catch (IOException e1) {
                         }
                     }
                 }
             }
         }
    }
    
    @Override
    public List<Porxy> getPorxyList(String filePath) {

        if( null != filePath ){
            File file = new File(filePath);
            List<Porxy> porxyList = new ArrayList<Porxy>();
            if( null != file ){
                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new FileReader(file));
                    String tempString = null;
                    // 一次读入一行，直到读入null为文件结束
                    while ((tempString = reader.readLine()) != null) {
                        if( null != tempString ){
                            String ip = tempString.substring(0, tempString.indexOf(":"));
                            int port = Integer.parseInt( tempString.substring(tempString.indexOf(":")+1, tempString.length()));
                            if( ip != null && port != 0 ){
                                Porxy porxy = new Porxy();
                                porxy.setIp(ip);
                                porxy.setPort(port);
                                porxyList.add(porxy);
                            }
                        }
                    }
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (IOException e1) {
                        }
                    }
                }
            }
            return porxyList;
        }
        return null;
    }
    
    public static void main(String args[]){
        PorxyDao dao = new PorxyDaoImpl();
        dao.importPorxyList("d://porxy.txt");
    }

   

}
